home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / samples / sphere.c.z / sphere.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  16.5 KB  |  991 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include "tk.h"
  6.  
  7.  
  8. #define FALSE 0
  9. #define TRUE  1
  10. #define PI    3.14159265358979323846
  11.  
  12.  
  13. GLenum doubleBuffer, directRender;
  14. int W = 400, H = 400;
  15.  
  16. char *imageFileName = 0;
  17. TK_RGBImageRec *image;
  18.  
  19. int numComponents;
  20.  
  21. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  22. float decal[] = {GL_DECAL};
  23. float modulate[] = {GL_MODULATE};
  24. float repeat[] = {GL_REPEAT};
  25. float clamp[] = {GL_CLAMP};
  26. float nearest[] = {GL_NEAREST};
  27. float linear[] = {GL_LINEAR};
  28. float nearest_mipmap_nearest[] = {GL_NEAREST_MIPMAP_NEAREST};
  29. float nearest_mipmap_linear[] = {GL_NEAREST_MIPMAP_LINEAR};
  30. float linear_mipmap_nearest[] = {GL_LINEAR_MIPMAP_NEAREST};
  31. float linear_mipmap_linear[] = {GL_LINEAR_MIPMAP_LINEAR};
  32. GLint sphereMap[] = {GL_SPHERE_MAP};
  33.  
  34. float xRotation = 0.0, yRotation = 0.0;
  35. float zTranslate = -4.0;
  36. GLenum autoRotate = TRUE;
  37. GLenum deepestColor = TK_GREEN;
  38. GLenum isLit = TRUE;
  39. GLenum isFogged = FALSE;
  40. float *textureEnvironment = modulate;
  41.  
  42. struct MipMap {
  43.     int width, height;
  44.     unsigned char *data;
  45. };
  46.  
  47. int cube, cage, cylinder, torus, genericObject;
  48.  
  49. float c[6][4][4][3] = {
  50.     {
  51.     {
  52.         {
  53.         1.0, 1.0, -1.0
  54.         }, 
  55.         {
  56.         0.0, 1.0, -1.0
  57.         },
  58.         {
  59.         0.0, 0.0, -1.0
  60.         },
  61.         {
  62.         1.0, 0.0, -1.0
  63.         },
  64.     },
  65.     {
  66.         {
  67.         0.0, 1.0, -1.0
  68.         },
  69.         {
  70.         -1.0, 1.0, -1.0
  71.         }, 
  72.         {
  73.         -1.0, 0.0, -1.0
  74.         }, 
  75.         {
  76.         0.0, 0.0, -1.0
  77.         },
  78.     },
  79.     {
  80.         {
  81.         0.0,  0.0, -1.0
  82.         },
  83.         {
  84.         -1.0, 0.0, -1.0
  85.         },
  86.         {
  87.         -1.0, -1.0, -1.0
  88.         },
  89.         {
  90.         0.0, -1.0, -1.0
  91.         },
  92.     },
  93.     {
  94.         {
  95.         1.0, 0.0, -1.0
  96.         },
  97.         {
  98.         0.0, 0.0, -1.0
  99.         },
  100.         {
  101.         0.0, -1.0, -1.0
  102.         },
  103.         {
  104.         1.0, -1.0, -1.0
  105.         },
  106.     },
  107.     },
  108.     {
  109.     {
  110.         {
  111.         1.0, 1.0, 1.0
  112.         },
  113.         {
  114.         1.0, 1.0, 0.0
  115.         },
  116.         {
  117.         1.0, 0.0, 0.0
  118.         },
  119.         {
  120.         1.0, 0.0, 1.0
  121.         },
  122.     },
  123.     {
  124.         {
  125.         1.0, 1.0, 0.0
  126.         },
  127.         {
  128.         1.0, 1.0, -1.0
  129.         },
  130.         {
  131.         1.0, 0.0, -1.0
  132.         },
  133.         {
  134.         1.0, 0.0, 0.0
  135.         },
  136.     },
  137.     {
  138.         {
  139.         1.0, 0.0, -1.0
  140.         },
  141.         {
  142.         1.0, -1.0, -1.0
  143.         },
  144.         {
  145.         1.0, -1.0, 0.0
  146.         },
  147.         {
  148.         1.0, 0.0, 0.0
  149.         },
  150.     },
  151.     {
  152.         {
  153.         1.0, 0.0, 0.0
  154.         },
  155.         {
  156.         1.0, -1.0, 0.0
  157.         },
  158.         {
  159.         1.0, -1.0, 1.0
  160.         },
  161.         {
  162.         1.0, 0.0, 1.0
  163.         },
  164.     },
  165.     },
  166.     {
  167.     {
  168.         {
  169.         -1.0, 1.0, 1.0
  170.         },
  171.         {
  172.         0.0, 1.0, 1.0
  173.         },
  174.         {
  175.         0.0, 0.0, 1.0
  176.         },
  177.         {
  178.         -1.0, 0.0, 1.0
  179.         },
  180.     },
  181.     {
  182.         {
  183.         0.0, 1.0, 1.0
  184.         },
  185.         {
  186.         1.0, 1.0, 1.0
  187.         },
  188.         {
  189.         1.0, 0.0, 1.0
  190.         },
  191.         {
  192.         0.0, 0.0, 1.0
  193.         },
  194.     },
  195.     {
  196.         {
  197.         1.0, 0.0, 1.0
  198.         },
  199.         {
  200.         1.0, -1.0, 1.0
  201.         },
  202.         {
  203.         0.0, -1.0, 1.0
  204.         },
  205.         {
  206.         0.0, 0.0, 1.0
  207.         },
  208.     },
  209.     {
  210.         {
  211.         0.0, -1.0, 1.0
  212.         },
  213.         {
  214.         -1.0, -1.0, 1.0
  215.         },
  216.         {
  217.         -1.0, 0.0, 1.0
  218.         },
  219.         {
  220.         0.0, 0.0, 1.0
  221.         },
  222.     },
  223.     },
  224.     {
  225.     {
  226.         {
  227.         -1.0, 1.0, -1.0
  228.         },
  229.         {
  230.         -1.0, 1.0, 0.0
  231.         },
  232.         {
  233.         -1.0, 0.0, 0.0
  234.         },
  235.         {
  236.         -1.0, 0.0, -1.0
  237.         },
  238.     }, 
  239.     {
  240.         {
  241.         -1.0, 1.0, 0.0
  242.         },
  243.         {
  244.         -1.0, 1.0, 1.0
  245.         },
  246.         {
  247.         -1.0, 0.0, 1.0
  248.         },
  249.         {
  250.         -1.0, 0.0, 0.0
  251.         },
  252.     }, 
  253.     {
  254.         {
  255.         -1.0, 0.0, 1.0
  256.         },
  257.         {
  258.         -1.0, -1.0, 1.0
  259.         },
  260.         {
  261.         -1.0, -1.0, 0.0
  262.         },
  263.         {
  264.         -1.0, 0.0, 0.0
  265.         },
  266.     }, 
  267.     {
  268.         {
  269.         -1.0, -1.0, 0.0
  270.         },
  271.         {
  272.         -1.0, -1.0, -1.0
  273.         },
  274.         {
  275.         -1.0, 0.0, -1.0
  276.         },
  277.         {
  278.         -1.0, 0.0, 0.0
  279.         },
  280.     }, 
  281.     },
  282.     {
  283.     {
  284.         {
  285.         -1.0, 1.0, 1.0
  286.         },
  287.         {
  288.         -1.0, 1.0, 0.0
  289.         },
  290.         {
  291.         0.0, 1.0, 0.0
  292.         },
  293.         {
  294.         0.0, 1.0, 1.0
  295.         },
  296.     },
  297.     {
  298.         {
  299.         -1.0, 1.0, 0.0
  300.         },
  301.         {
  302.         -1.0, 1.0, -1.0
  303.         },
  304.         {
  305.         0.0, 1.0, -1.0
  306.         },
  307.         {
  308.         0.0, 1.0, 0.0
  309.         },
  310.     },
  311.     {
  312.         {
  313.         0.0, 1.0, -1.0
  314.         },
  315.         {
  316.         1.0, 1.0, -1.0
  317.         },
  318.         {
  319.         1.0, 1.0, 0.0
  320.         },
  321.         {
  322.         0.0, 1.0, 0.0
  323.         },
  324.     },
  325.     {
  326.         {
  327.         1.0, 1.0, 0.0
  328.         },
  329.         {
  330.         1.0, 1.0, 1.0
  331.         },
  332.         {
  333.         0.0, 1.0, 1.0
  334.         },
  335.         {
  336.         0.0, 1.0, 0.0
  337.         },
  338.     },
  339.     },
  340.     {
  341.     {
  342.         {
  343.         -1.0, -1.0, -1.0
  344.         },
  345.         {
  346.         -1.0, -1.0, 0.0
  347.         },
  348.         {
  349.         0.0, -1.0, 0.0
  350.         },
  351.         {
  352.         0.0, -1.0, -1.0
  353.         },
  354.     },
  355.     {
  356.         {
  357.         -1.0, -1.0, 0.0
  358.         },
  359.         {
  360.         -1.0, -1.0, 1.0
  361.         },
  362.         {
  363.         0.0, -1.0, 1.0
  364.         },
  365.         {
  366.         0.0, -1.0, 0.0
  367.         },
  368.     },
  369.     {
  370.         {
  371.         0.0, -1.0, 1.0
  372.         },
  373.         {
  374.         1.0, -1.0, 1.0
  375.         },
  376.         {
  377.         1.0, -1.0, 0.0
  378.         },
  379.         {
  380.         0.0, -1.0, 0.0
  381.         },
  382.     },
  383.     {
  384.         {
  385.         1.0, -1.0, 0.0
  386.         },
  387.         {
  388.         1.0, -1.0, -1.0
  389.         },
  390.         {
  391.         0.0, -1.0, -1.0
  392.         },
  393.         {
  394.         0.0, -1.0, 0.0
  395.         },
  396.     },
  397.     }
  398. };
  399.  
  400. float n[6][3] = {
  401.     {
  402.     0.0, 0.0, -1.0
  403.     },
  404.     {
  405.     1.0, 0.0, 0.0
  406.     },
  407.     {
  408.     0.0, 0.0, 1.0
  409.     },
  410.     {
  411.     -1.0, 0.0, 0.0
  412.     },
  413.     {
  414.     0.0, 1.0, 0.0
  415.     },
  416.     {
  417.     0.0, -1.0, 0.0
  418.     }
  419. };
  420.  
  421. GLfloat identity[16] = {
  422.     1, 0, 0, 0,
  423.     0, 1, 0, 0,
  424.     0, 0, 1, 0,
  425.     0, 0, 0, 1,
  426. };
  427.  
  428.  
  429. void BuildCylinder(int numEdges)
  430. {
  431.     int i, top = 1.0, bottom = -1.0;
  432.     float x[100], y[100], angle; 
  433.     
  434.     for (i = 0; i <= numEdges; i++) {
  435.     angle = i * 2.0 * PI / numEdges;
  436.     x[i] = cosf(angle);
  437.     y[i] = sinf(angle);
  438.     }
  439.  
  440.     glNewList(cylinder, GL_COMPILE);
  441.     glBegin(GL_TRIANGLE_STRIP);
  442.     for (i = 0; i <= numEdges; i++) {
  443.         glNormal3f(x[i], y[i], 0.0);
  444.         glVertex3f(x[i], y[i], bottom);
  445.         glVertex3f(x[i], y[i], top);
  446.     }
  447.     glEnd();
  448.     glBegin(GL_TRIANGLE_FAN);
  449.     glNormal3f(0.0, 0.0, 1.0);
  450.     glVertex3f(0.0, 0.0, top);
  451.     for (i = 0; i <= numEdges; i++) {
  452.         glVertex3f(x[i], -y[i], top);
  453.     }
  454.     glEnd();
  455.     glBegin(GL_TRIANGLE_FAN);
  456.     glNormal3f(0.0, 0.0, -1.0);
  457.     glVertex3f(0.0, 0.0, bottom);
  458.     for (i = 0; i <= numEdges; i++) {
  459.         glVertex3f(x[i], y[i], bottom);
  460.     }
  461.     glEnd();
  462.     glEndList();
  463. }
  464.  
  465. void BuildTorus(float rc, int numc, float rt, int numt)
  466. {
  467.     int i, j, k;
  468.     double s, t;
  469.     double x, y, z;
  470.     double pi, twopi;
  471.  
  472.     pi = 3.14159265358979323846;
  473.     twopi = 2.0 * pi;
  474.  
  475.     glNewList(torus, GL_COMPILE);
  476.     for (i = 0; i < numc; i++) {
  477.     glBegin(GL_QUAD_STRIP);
  478.         for (j = 0; j <= numt; j++) {
  479.         for (k = 0; k <= 1; k++) {
  480.         s = (i + k) % numc + 0.5;
  481.         t = j % numt;
  482.  
  483.         x = cos(t*twopi/numt) * cos(s*twopi/numc);
  484.         y = sin(t*twopi/numt) * cos(s*twopi/numc);
  485.         z = sin(s*twopi/numc);
  486.         glNormal3f(x, y, z);
  487.  
  488.         x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  489.         y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  490.         z = rc * sin(s*twopi/numc);
  491.         glVertex3f(x, y, z);
  492.         }
  493.         }
  494.     glEnd();
  495.     }
  496.     glEndList();
  497. }
  498.  
  499. void BuildCage(void)
  500. {
  501.     int i, j;
  502.     float inc;
  503.     float right, left, top, bottom, front, back;
  504.  
  505.     front  = 0.0;
  506.     back   = -8.0;
  507.  
  508.     left   = -4.0;
  509.     bottom = -4.0;
  510.     right  = 4.0;
  511.     top    = 4.0; 
  512.  
  513.     inc = 2.0 * 4.0 * 0.1;
  514.  
  515.     glNewList(cage, GL_COMPILE);
  516.     for (i = 0; i < 10; i++) {
  517.  
  518.     /*
  519.     ** Back
  520.     */
  521.     glBegin(GL_LINES);
  522.         glVertex3f(left+i*inc, top,    back);
  523.         glVertex3f(left+i*inc, bottom, back);
  524.     glEnd();
  525.     glBegin(GL_LINES);
  526.         glVertex3f(right, bottom+i*inc, back);
  527.         glVertex3f(left,  bottom+i*inc, back);
  528.     glEnd();
  529.  
  530.     /*
  531.     ** Front
  532.     */
  533.     glBegin(GL_LINES);
  534.         glVertex3f(left+i*inc, top,    front);
  535.         glVertex3f(left+i*inc, bottom, front);
  536.     glEnd();
  537.     glBegin(GL_LINES);
  538.         glVertex3f(right, bottom+i*inc, front);
  539.         glVertex3f(left,  bottom+i*inc, front);
  540.     glEnd();
  541.  
  542.     /*
  543.     ** Left
  544.     */
  545.     glBegin(GL_LINES);
  546.         glVertex3f(left, bottom+i*inc, front);
  547.         glVertex3f(left, bottom+i*inc, back);
  548.     glEnd();
  549.     glBegin(GL_LINES);
  550.         glVertex3f(left, top,    back+i*inc);
  551.         glVertex3f(left, bottom, back+i*inc);
  552.     glEnd();
  553.  
  554.     /*
  555.     ** Right
  556.     */
  557.     glBegin(GL_LINES);
  558.         glVertex3f(right, top-i*inc, front);
  559.         glVertex3f(right, top-i*inc, back);
  560.     glEnd();
  561.     glBegin(GL_LINES);
  562.         glVertex3f(right, top,    back+i*inc);
  563.         glVertex3f(right, bottom, back+i*inc);
  564.     glEnd();
  565.  
  566.     /*
  567.     ** Top
  568.     */
  569.     glBegin(GL_LINES);
  570.         glVertex3f(left+i*inc, top, front);
  571.         glVertex3f(left+i*inc, top, back);
  572.     glEnd();
  573.     glBegin(GL_LINES);
  574.         glVertex3f(right, top, back+i*inc);
  575.         glVertex3f(left,  top, back+i*inc);
  576.     glEnd();
  577.  
  578.     /*
  579.     ** Bottom
  580.     */
  581.     glBegin(GL_LINES);
  582.         glVertex3f(right-i*inc, bottom, front);
  583.         glVertex3f(right-i*inc, bottom, back);
  584.     glEnd();
  585.     glBegin(GL_LINES);
  586.         glVertex3f(right, bottom, back+i*inc);
  587.         glVertex3f(left,  bottom, back+i*inc);
  588.     glEnd();
  589.     }
  590.     glEndList();
  591. }
  592.  
  593. void BuildCube(void)
  594. {
  595.     int i, j;
  596.  
  597.     glNewList(cube, GL_COMPILE);
  598.     for (i = 0; i < 6; i++) {
  599.     for (j = 0; j < 4; j++) {
  600.         glNormal3fv(n[i]); 
  601.         glBegin(GL_POLYGON);
  602.         glVertex3fv(c[i][j][0]);
  603.         glVertex3fv(c[i][j][1]);
  604.         glVertex3fv(c[i][j][2]);
  605.         glVertex3fv(c[i][j][3]);
  606.         glEnd();
  607.     }
  608.     }
  609.     glEndList();
  610. }
  611.  
  612. void BuildLists(void)
  613. {
  614.  
  615.     cube = glGenLists(1);
  616.     BuildCube();
  617.  
  618.     cage = glGenLists(2);
  619.     BuildCage();
  620.  
  621.     cylinder = glGenLists(3);
  622.     BuildCylinder(60);
  623.  
  624.     torus = glGenLists(4);
  625.     BuildTorus(0.65, 20, .85, 65);
  626.  
  627.     genericObject = torus;
  628. }
  629.  
  630. void SetDeepestColor(void)
  631. {
  632.     GLint redBits, greenBits, blueBits;
  633.  
  634.     glGetIntegerv(GL_RED_BITS, &redBits);
  635.     glGetIntegerv(GL_GREEN_BITS, &greenBits);
  636.     glGetIntegerv(GL_BLUE_BITS, &blueBits);
  637.  
  638.     deepestColor = (redBits >= greenBits) ? TK_RED : TK_GREEN;
  639.     deepestColor = (deepestColor >= blueBits) ? deepestColor : TK_BLUE; 
  640. }
  641.  
  642. void SetDefaultSettings(void)
  643. {
  644.  
  645.     magFilter = nearest;
  646.     minFilter = nearest;
  647.     sWrapMode = repeat;
  648.     tWrapMode = repeat;
  649.     textureEnvironment = modulate;
  650.     autoRotate = TRUE;
  651. }
  652.  
  653. unsigned char *AlphaPadImage(int bufSize, unsigned char *inData, int alpha)
  654. {
  655.     unsigned char *outData, *out_ptr, *in_ptr;
  656.     int i;
  657.  
  658.     outData = malloc(bufSize * 4);
  659.     out_ptr = outData;
  660.     in_ptr = inData;
  661.  
  662.     for (i = 0; i < bufSize; i++) {
  663.     *out_ptr++ = *in_ptr++;
  664.     *out_ptr++ = *in_ptr++;
  665.     *out_ptr++ = *in_ptr++;
  666.     *out_ptr++ = alpha;
  667.     }
  668.  
  669.     free (inData);
  670.     return outData;
  671. }
  672.  
  673. void Init(void)
  674. {
  675.     float ambient[] = {0.0, 0.0, 0.0, 1.0};
  676.     float diffuse[] = {0.0, 1.0, 0.0, 1.0};
  677.     float specular[] = {1.0, 1.0, 1.0, 1.0};
  678.     float position[] = {2.0, 2.0,  0.0, 1.0};
  679.     float fog_color[] = {0.0, 0.0, 0.0, 1.0};
  680.     float mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  681.     float mat_shininess[] = {90.0};
  682.     float mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  683.     float mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  684.     float lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0};
  685.     float lmodel_twoside[] = {GL_TRUE};
  686.     float lmodel_local_viewer[] = {GL_FALSE};
  687.  
  688.     SetDeepestColor();
  689.     SetDefaultSettings();
  690.  
  691.     if (numComponents == 4) {
  692.     image = tkRGBImageLoad(imageFileName);
  693.     image->data = AlphaPadImage(image->sizeX*image->sizeY,
  694.                                     image->data, 128);
  695.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  696.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  697.               image->sizeX, image->sizeY, 
  698.               GL_RGBA, GL_UNSIGNED_BYTE, image->data);
  699.     } else {
  700.     image = tkRGBImageLoad(imageFileName);
  701.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  702.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  703.               image->sizeX, image->sizeY, 
  704.               GL_RGB, GL_UNSIGNED_BYTE, image->data);
  705.     }
  706.     
  707.     glFogf(GL_FOG_DENSITY, 0.125);
  708.     glFogi(GL_FOG_MODE, GL_LINEAR);
  709.     glFogf(GL_FOG_START, 4.0);
  710.     glFogf(GL_FOG_END, 9.0);
  711.     glFogfv(GL_FOG_COLOR, fog_color);
  712.  
  713.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  714.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  715.     glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  716.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  717.     
  718.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  719.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  720.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  721.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  722.  
  723.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  724.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  725.     glShadeModel(GL_SMOOTH);
  726.  
  727.     glEnable(GL_LIGHTING);
  728.     glEnable(GL_LIGHT0);
  729.  
  730.     glClearColor(0.0, 0.0, 0.0, 0.0);
  731.     glViewport(0, 0, W, H);
  732.     glEnable(GL_DEPTH_TEST);
  733.  
  734.     glFrontFace(GL_CW);
  735.     glEnable(GL_CULL_FACE);
  736.     glCullFace(GL_BACK);
  737.  
  738.     glEnable(GL_TEXTURE_2D);
  739.     glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  740.     glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  741.     glEnable(GL_TEXTURE_GEN_S);
  742.     glEnable(GL_TEXTURE_GEN_T);
  743.  
  744.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  745.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  746.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  747.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  748.  
  749.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  750.  
  751.     BuildLists();
  752. }
  753.  
  754. void ReInit(void)
  755. {
  756.  
  757.     if (genericObject == torus) {
  758.     glEnable(GL_DEPTH_TEST);
  759.     } else  {
  760.     glDisable(GL_DEPTH_TEST);
  761.     }
  762.     if (isFogged) {
  763.     textureEnvironment = modulate;
  764.     }
  765.  
  766.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  767.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  768.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  769. }
  770.  
  771. void Draw(void)
  772. {
  773.  
  774.     glMatrixMode(GL_PROJECTION);
  775.     glLoadIdentity();
  776.     glFrustum(-0.2, 0.2, -0.2, 0.2, 0.15, 9.0);
  777.     glMatrixMode(GL_MODELVIEW);
  778.  
  779.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  780.     if (isFogged) {
  781.     glEnable(GL_FOG);
  782.     glColor3fv(tkRGBMap[deepestColor]);
  783.     } else {
  784.     glColor3fv(tkRGBMap[TK_WHITE]);
  785.     }
  786.     glDisable(GL_LIGHTING);
  787.     glDisable(GL_LIGHT0);
  788.     glDisable(GL_TEXTURE_2D);
  789.     glCallList(cage);
  790.  
  791.     glPushMatrix();
  792.     glTranslatef(0.0, 0.0, zTranslate);
  793.     glRotatef(xRotation, 1, 0, 0);
  794.     glRotatef(yRotation, 0, 1, 0);
  795.  
  796.     if (isLit == TRUE) {
  797.     glEnable(GL_LIGHTING);
  798.     glEnable(GL_LIGHT0);
  799.     }
  800.  
  801.     glEnable(GL_TEXTURE_2D);
  802.     if (isFogged) {
  803.     glDisable(GL_FOG);
  804.     }
  805.     glPolygonMode(GL_FRONT, GL_FILL);
  806.     glColor3fv(tkRGBMap[deepestColor]);
  807.     glCallList(genericObject);
  808.  
  809.     glPopMatrix();
  810.     glFlush();
  811.  
  812.     if (autoRotate) {
  813.     xRotation += .75;
  814.     yRotation += .375;
  815.     } 
  816.     tkSwapBuffers();
  817. }
  818.  
  819. void Reshape(int width, int height)
  820. {
  821.  
  822.     W = width;
  823.     H = height;
  824.     ReInit();
  825. }
  826.  
  827. GLenum Key(int key, GLenum mask)
  828. {
  829.  
  830.     switch (key) {
  831.       case TK_ESCAPE:
  832.     free(image->data);
  833.     tkQuit();
  834.  
  835.       case TK_LEFT:
  836.     yRotation -= 0.5;
  837.     autoRotate = FALSE;
  838.     ReInit();
  839.     break;
  840.       case TK_RIGHT:
  841.     yRotation += 0.5;
  842.     autoRotate = FALSE;
  843.     ReInit();
  844.     break;
  845.       case TK_UP:
  846.     xRotation -= 0.5;
  847.     autoRotate = FALSE;
  848.     ReInit();
  849.     break;
  850.       case TK_DOWN:
  851.     xRotation += 0.5;
  852.     autoRotate = FALSE;
  853.     ReInit();
  854.     break;
  855.       case TK_a:
  856.     autoRotate = !autoRotate;
  857.     ReInit();
  858.     break;
  859.       case TK_c:
  860.     genericObject = (genericObject == cube) ? cylinder : cube;
  861.     ReInit();
  862.     break;
  863.       case TK_d:
  864.     textureEnvironment = decal;
  865.     ReInit();
  866.     break;
  867.       case TK_m:
  868.     textureEnvironment = modulate;
  869.     ReInit();
  870.     break;
  871.       case TK_l:
  872.     isLit = !isLit;
  873.     ReInit();
  874.     break;
  875.       case TK_f:
  876.     isFogged = !isFogged;
  877.     ReInit();
  878.     break;
  879.       case TK_t:
  880.     genericObject = torus;
  881.     ReInit();
  882.     break;
  883.       case TK_0:
  884.     magFilter = nearest;
  885.     ReInit();
  886.     break;
  887.       case TK_1:
  888.     magFilter = linear;
  889.     ReInit();
  890.     break;
  891.       case TK_2:
  892.     minFilter = nearest;
  893.     ReInit();
  894.     break;
  895.       case TK_3:
  896.     minFilter = linear;
  897.     ReInit();
  898.     break;
  899.       case TK_4:
  900.     minFilter = nearest_mipmap_nearest;
  901.     ReInit();
  902.     break;
  903.       case TK_5:
  904.     minFilter = nearest_mipmap_linear;
  905.     ReInit();
  906.     break;
  907.       case TK_6:
  908.     minFilter = linear_mipmap_nearest;
  909.     ReInit();
  910.     break;
  911.       case TK_7:
  912.     minFilter = linear_mipmap_linear;
  913.     ReInit();
  914.     break;
  915.  
  916.       default:
  917.     return GL_FALSE;
  918.     }
  919.     return GL_TRUE;
  920. }
  921.  
  922. GLenum Args(int argc, char **argv)
  923. {
  924.     GLint i;
  925.  
  926.     doubleBuffer = GL_FALSE;
  927.     directRender = GL_TRUE;
  928.     numComponents = 4;
  929.  
  930.     for (i = 1; i < argc; i++) {
  931.     if (strcmp(argv[i], "-sb") == 0) {
  932.         doubleBuffer = GL_FALSE;
  933.     } else if (strcmp(argv[i], "-db") == 0) {
  934.         doubleBuffer = GL_TRUE;
  935.     } else if (strcmp(argv[i], "-dr") == 0) {
  936.         directRender = GL_TRUE;
  937.     } else if (strcmp(argv[i], "-ir") == 0) {
  938.         directRender = GL_FALSE;
  939.     } else if (strcmp(argv[i], "-f") == 0) {
  940.         if (i+1 >= argc || argv[i+1][0] == '-') {
  941.         printf("-f (No file name).\n");
  942.         return GL_FALSE;
  943.         } else {
  944.         imageFileName = argv[++i];
  945.         }
  946.     } else if (strcmp(argv[i], "-4") == 0) {
  947.         numComponents = 4;
  948.     } else if (strcmp(argv[i], "-3") == 0) {
  949.         numComponents = 3;
  950.     } else {
  951.         printf("%s (Bad option).\n", argv[i]);
  952.         return GL_FALSE;
  953.     }
  954.     }
  955.     return GL_TRUE;
  956. }
  957.  
  958. void main(int argc, char **argv)
  959. {
  960.     GLenum type;
  961.  
  962.     if (Args(argc, argv) == GL_FALSE) {
  963.     tkQuit();
  964.     }
  965.  
  966.     if (imageFileName == 0) {
  967.     printf("No image file.\n");
  968.     tkQuit();
  969.     }
  970.  
  971.     tkInitPosition(0, 0, W, H);
  972.  
  973.     type = TK_RGB | TK_DEPTH;
  974.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  975.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  976.     tkInitDisplayMode(type);
  977.  
  978.     if (tkInitWindow("Texture Test") == GL_FALSE) {
  979.         tkQuit();
  980.     }
  981.  
  982.     Init();
  983.  
  984.     tkExposeFunc(Reshape);
  985.     tkReshapeFunc(Reshape);
  986.     tkKeyDownFunc(Key);
  987.     tkIdleFunc(Draw);
  988.  
  989.     tkExec();
  990. }
  991.